﻿using Library.Core.Domain.Specification;
using Synthesize.BaseSet.Domain.Aggregation.Entity.Authority;
using Synthesize.BaseSet.Domain.Aggregation.ValueObject.Authority;
using System;
using System.Linq.Expressions;

namespace Synthesize.BaseSet.Domain.Specifications.Authority.Menu
{

    /// <summary>
    /// 菜单查询条件拼装
    /// </summary>
    public class GetMenuListSpceification : ISpecification<MenuInfoEntity>
    {
        private readonly string _name;
        private readonly MenuPlatformEnum? _platform;

        public GetMenuListSpceification(string name, MenuPlatformEnum? platform)
        {
            _name = name;
            _platform = platform;
        }

        public Expression<Func<MenuInfoEntity, bool>> SatisfiedBy()
        {
            Specification<MenuInfoEntity> spec = new TrueSpecification<MenuInfoEntity>();

            if (_platform.HasValue)
            {
                spec &= new DirectSpecification<MenuInfoEntity>(x => x.Platform == _platform);
            }

            if (!string.IsNullOrEmpty(_name))
            {
                spec &= new DirectSpecification<MenuInfoEntity>(x => x.Name.Contains(_name.Trim()) || x.Title.Contains(_name.Trim()));
            }

            return spec.SatisfiedBy();
        }
    }
}
